package com.gdou.crrp.controller;

import com.gdou.crrp.entity.*;
import com.gdou.crrp.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    /**
     * 跳转到预约页面
     * @param model ：所有医院
     * @return 跳转reserve.html
     */
    @RequestMapping("/toReserve")
    public String toReserve(Model model){
        List<Hospital> allHospital = reservationService.findAllHospital();
        model.addAttribute("allHospital", allHospital);
        return "reserve";
    }

    /**
     * 选择医院后，发送科室列表到reserve.html
     * @param name 医院名称
     * @return 科室列表
     */
    @ResponseBody
    @PostMapping("/selectDepartment")
    public List<Department> selectDepartment(@RequestParam String name){
        return reservationService.findDepartmentByHospitalName(name);
    }

    /**
     * 选择科室后，发送医生列表到reserve.html
     * @param hname 医院名称
     * @param dname 科室名称
     * @return 医生列表
     */
    @ResponseBody
    @PostMapping("/selectDoctor")
    public List<Doctor> selectDoctor(@RequestParam(name = "hname") String hname, @RequestParam(name = "dname") String dname){
        return reservationService.findDoctorByHnameAndDname(hname, dname);
    }

    /**
     * 选择医生后，发送可预约时间列表到reserve.html
     * @param hname 医院名称
     * @param dname 科室名称
     * @param name 医生姓名
     * @return 可预约时间列表
     */
    @ResponseBody
    @PostMapping("/selectTime")
    public Map<String, Object> selectTime(@RequestParam(name = "hname") String hname, @RequestParam(name = "dname") String dname, @RequestParam(name = "name") String name){
        Doctor doctor = reservationService.findDoctorByHnameAndDnameAndName(hname, dname, name);
        List<Date> afterWeek = reservationService.getDoctorWorkTime(doctor);
        Map<String, Object> map = new HashMap<>();
        map.put("doctor", doctor);
        map.put("afterWeek", afterWeek);
        return map;
    }

    /**
     * 进行预约，生成预约单
     * @param hname 医院名称
     * @param dname 科室名称
     * @param name 医生姓名
     * @param time 预约时间
     * @return 预约单
     */
    @ResponseBody
    @PostMapping("/reserve")
    public Reservation reserve(@RequestParam(name = "hospitalName") String hname, @RequestParam(name = "departmentName") String dname, @RequestParam(name = "doctorName") String name, @RequestParam(name = "time") Date time, HttpSession session){
        String user = session.getAttribute("phone").toString();
        return reservationService.save(user, hname, dname, name, time);
    }

    /**
     * 进行预约付款
     * @param id 预约单
     * @param pay 费用
     * @return 付款信息（成功与否）
     */
    @ResponseBody
    @PostMapping("/pay")
    public String pay(Integer id, Integer pay){
        return reservationService.pay(id, pay);
    }

    /**
     * 跳转我的订单页面（bill.html）
     * @param model 我的订单列表
     * @return 我的订单列表
     */
    @RequestMapping("/toBill")
    public String toBill(HttpSession session, Model model){
        Integer id = (Integer) session.getAttribute("id");
        List<Bill> billList = reservationService.findBillByUid(id);
        model.addAttribute("billList",billList);
        return "bill";
    }

    /**
     * 获取订单详细信息
     * @param id 订单id
     * @return 订单号为id的详细信息
     */
    @ResponseBody
    @PostMapping("/getBillInfo")
    public Reservation getBillInfo(@RequestParam Integer id){
        return reservationService.findReservationById(id);
    }

    /**
     * 跳转到充值页面（recharge.html）
     */
    @RequestMapping("/toRecharge")
    public String toRecharge(){
        return "recharge";
    }

    /**
     * 进行充值
     * @param money 充值金额
     * @return 充值信息（成功与否）
     */
    @ResponseBody
    @PostMapping("/recharge")
    public String recharge(int money, HttpSession session) {
        Integer id = (Integer) session.getAttribute("id");
        return reservationService.recharge(id, money);
    }

    /**
     * 跳转我的钱包
     * @param model 钱包信息
     */
    @RequestMapping("toWallet")
    public String toWallet(Model model, HttpSession session){
        Integer id = (Integer) session.getAttribute("id");
        User user = reservationService.getUserById(id);
        model.addAttribute("money", user.getMoney());
        return "wallet";
    }

    /**
     * 模糊查找医院
     * @param name 模糊的医院名称
     * @return 符合要求的医院
     */
    @ResponseBody
    @PostMapping("/findHospital")
    public List<Hospital> findHospital(String name){
         return reservationService.findHospital(name);
    }

    /**
     * 模糊查找科室
     * @param hname 医院名称
     * @param name 模糊的科室名称
     * @return 符合要求的科室
     */
    @ResponseBody
    @PostMapping("/findDepartment")
    public List<Department> findDepartment(String hname, String name){
        return reservationService.findDepartment(hname, name);
    }

    /**
     * 取消预约，删除相关订单与预约单
     * @param id 预约单id
     * @return 重定向到我的订单页面
     */
    @RequestMapping("/cancelReserve")
    public String cancelReserve(@RequestParam(name = "id") int id){
        reservationService.cancelReserve(id);
        return "redirect:/reservation/toBill";
    }

}
